package com.itheima.service.db;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.domain.db.AnalysisByDay;
import com.itheima.mapper.AnalysisByDayMapper;
import com.itheima.mapper.LogMapper;
import com.itheima.util.ComputeUtil;
import com.itheima.vo.AnalysisSummaryVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.Date;

@DubboService
public class AnalysisByDayServiceImpl implements AnalysisByDayService {

    @Autowired
    AnalysisByDayMapper analysisByDayMapper;

    @Autowired
    LogMapper logMapper;

    @Override
    public void saveOrUpdate() {
       //1.先通过今天日期查询数据库中有无记录

        //1.0 准备基础数据(今天字符串,昨天字符串)
        String today = DateUtil.offsetDay(new Date(), 0).toDateStr();
        String yesterday = DateUtil.offsetDay(new Date(), -1).toDateStr();

        //1.1 创建条件对象
        QueryWrapper<AnalysisByDay> wrapper = new QueryWrapper<>();
        wrapper.eq("record_date", today);

        //1.2 查询数据
        AnalysisByDay analysisByDay = analysisByDayMapper.selectOne(wrapper);
        if (analysisByDay != null) {
            //2.若有则更新
            //新增
            analysisByDay.setNumRegistered(logMapper.findTodayRegistOrLoginCount(today,"0102"));
            //登陆
            analysisByDay.setNumLogin(logMapper.findTodayRegistOrLoginCount(today,"0101"));
            //活跃
            analysisByDay.setNumActive(logMapper.findTodayActiveCount(today));
            //次日留存
            analysisByDay.setNumRetention1d(logMapper.findRetention1dCount(today,yesterday));

            //更新
            analysisByDayMapper.updateById(analysisByDay);
        }else{
            //3.若无则保存
            analysisByDay = new AnalysisByDay();
            //日期
            analysisByDay.setRecordDate(new Date());
            //新增
            analysisByDay.setNumRegistered(logMapper.findTodayRegistOrLoginCount(today,"0102"));
            //登陆
            analysisByDay.setNumLogin(logMapper.findTodayRegistOrLoginCount(today,"0101"));
            //活跃
            analysisByDay.setNumActive(logMapper.findTodayActiveCount(today));
            //次日留存
            analysisByDay.setNumRetention1d(logMapper.findRetention1dCount(today,yesterday));

            //保存
            analysisByDayMapper.insert(analysisByDay);
        }

    }

    @Override
    public AnalysisSummaryVo findSummaryVo() {
        //1.准备基础数据(今天,昨天,7天,30天)
        String today  = DateUtil.offsetDay(new Date(), 0).toDateStr();
        String yesterday  = DateUtil.offsetDay(new Date(), -1).toDateStr();
        String passWeek  = DateUtil.offsetDay(new Date(), -7).toDateStr();
        String passMonth  = DateUtil.offsetDay(new Date(), -30).toDateStr();

        //2.创建vo
        AnalysisSummaryVo vo = new AnalysisSummaryVo();

        //3.封装vo的9个字段
        //3.1 查询今天的数据
        AnalysisByDay todayAnalysis = analysisByDayMapper.findByRecordDate(today);

        //3.2 查询昨天的数据
        AnalysisByDay yesterdayAnalysis = analysisByDayMapper.findByRecordDate(yesterday);

        //3.3 封装各个数据
        //累计用户数
        vo.setCumulativeUsers(analysisByDayMapper.findTotalUserCount());
        //过去30天活跃用户数
        vo.setActivePassMonth(analysisByDayMapper.findActiveCountInPeriod(passMonth,yesterday));
        //过去7天活跃用户
        vo.setActivePassWeek(analysisByDayMapper.findActiveCountInPeriod(passWeek,yesterday));
        //今日新增用户数量
        vo.setNewUsersToday(todayAnalysis.getNumRegistered().longValue());
        //今日新增用户涨跌率，单位百分数，正数为涨，负数为跌   BigDecimal : 商业数字格式
        vo.setNewUsersTodayRate(ComputeUtil.computeRate(todayAnalysis.getNumRegistered(),yesterdayAnalysis.getNumRegistered()));
        //今日登录次数
        vo.setLoginTimesToday(todayAnalysis.getNumLogin().longValue());
        //今日登录次数涨跌率，单位百分数，正数为涨，负数为跌
        vo.setLoginTimesTodayRate(ComputeUtil.computeRate(todayAnalysis.getNumLogin(),yesterdayAnalysis.getNumLogin()));
        //今日活跃用户数量
        vo.setActiveUsersToday(todayAnalysis.getNumActive().longValue());
        //今日活跃用户涨跌率，单位百分数，正数为涨，负数为跌
        vo.setActiveUsersTodayRate(ComputeUtil.computeRate(todayAnalysis.getNumActive(),yesterdayAnalysis.getNumActive()));

        //4.返回
        return vo;
    }
}
